<?php

Raise::load('core.RaiseString');

/**
 * TestCoreRaiseString class
 * Testing unit of 'phpraise/core/RaiseString.php'
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.UnitTest.Test
 */
class TestCoreRaiseString extends RaiseTest {
    
    public static function testClass(){
        self::assertClassExists('RaiseString');
        self::assertClassExtends('RaiseString', 'RaiseDataType');
    }
    
    public static function testString1(){
        $z = 'AbC13aA';
        $s = new RaiseString($z);
        
        self::assertStrictEqual($s->__toString(), $s->value());
        self::assertStrictEqual($s->__toString(), $z);
        self::assertStrictEqual($s->toLower()->value(), 'abc13aa');
        self::assertStrictEqual($s->toUpper()->value(), 'ABC13AA');
        
        self::assertStrictEqual($s->length(), 7);
        self::assertStrictEqual($s->indexOf('C'), 2);
        self::assertStrictEqual($s->indexOf('A'), 0);
        self::assertStrictEqual($s->lastIndexOf('A'), 6);
        self::assertStrictEqual($s->indexOf('a'), $s->lastIndexOf('a'));
    }
    
    public static function testString2(){
        $z = '  AbC13a   ';
        $s = new RaiseString($z);
        
        self::assertStrictEqual($s->trim()->value(), 'AbC13a');
        self::assertStrictEqual($s->trimLeft()->value(), 'AbC13a   ');
        self::assertStrictEqual($s->trimRight()->value(), '  AbC13a');
    }
    
    public static function testString3(){
        $z = 'dc1:dc2:dc3:dc4:dc5';
        $s = new RaiseString($z);
        
        self::assertStrictEqual($s->split(':')->count(), 5);
        self::assertObjectClass($s->split(':'), 'RaiseTypedCollection');
        self::assertStrictEqual($s->split(':')->type(), 'RaiseString');
        self::assertObjectClass($s->split(':')->first(), 'RaiseString');
        self::assertStrictEqual($s->split(':')->first()->value(), 'dc1');
        self::assertStrictEqual($s->split(':')->get(3)->value(), 'dc4');
        self::assertStrictEqual($s->split(':')->last()->value(), 'dc5');
    }
    
    public static function testString4(){
        $z = 'dc1:dc2:dc3:dc4:dc5';
        $s = new RaiseString($z);
        
        self::assertObjectClass($s->replace('dc1', 'dc6'), 'RaiseString');
        self::assertStrictEqual($s->replace('dc1', 'dc6')->value(), 'dc6:dc2:dc3:dc4:dc5');
        self::assertObjectClass($s->replace(array('dc1', 'dc2'), array('dc3','dc7')), 'RaiseString');
        self::assertStrictEqual($s->replace(array('dc1', 'dc2'), array('dc3','dc7'))->value(), 'dc3:dc7:dc3:dc4:dc5');
        self::assertObjectClass($s->replace(new RaiseCollection('dc1', 'dc2'), new RaiseCollection('dc3','dc7')), 'RaiseString');
        self::assertStrictEqual($s->replace(new RaiseCollection('dc1', 'dc2'), new RaiseCollection('dc3','dc7'))->value(), 'dc3:dc7:dc3:dc4:dc5');
    }
    
    public static function testString5(){
        $z = 'dc1:dc2:dc3:dc4:dc5';
        $s = new RaiseString($z);
        
        self::assertObjectClass($s->occurances('dc'), 'RaiseNumericCollection');
        self::assertStrictEqual($s->occurances('dc')->count(), 5);
        self::assertStrictEqual($s->occurances('dc')->toArray(), array(0, 4, 8, 12, 16));
        
        self::assertObjectClass($s->substring(0,5), 'RaiseString');
        self::assertStrictEqual($s->substring(0, 5)->value(), 'dc1:d');
        self::assertStrictEqual($s->substring(5, 6)->value(), 'c2:dc3');
    }
    
    public static function testString6(){
        $z = 'de5';
        $s = new RaiseString($z);
        
        self::assertObjectClass($s->padBoth('0', 5), 'RaiseString');
        self::assertObjectClass($s->padLeft('0', 5), 'RaiseString');
        self::assertObjectClass($s->padRight('0', 5), 'RaiseString');
        self::assertStrictEqual($s->padBoth('0', 5)->value(), '0de50');
        self::assertStrictEqual($s->padLeft('0', 5)->value(), '00de5');
        self::assertStrictEqual($s->padRight('0', 5)->value(), 'de500');
        self::assertStrictEqual($s->padBoth('0', 6)->value(), '0de500');
        self::assertStrictEqual($s->padLeft('0', 6)->value(), '000de5');
        self::assertStrictEqual($s->padRight('0', 6)->value(), 'de5000');
    }
    
}
